package org.mobile.beans;

import java.security.NoSuchAlgorithmException;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.mobile.encryption.MD5;
import org.mobile.logic.UserLogic;

public class PassBean {
	private User user;
	private String oldPass;
	private String newPass;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getOldPass() {
		return oldPass;
	}

	public void setOldPass(String oldPass) {
		this.oldPass = oldPass;
	}

	public String getNewPass() {
		return newPass;
	}

	public void setNewPass(String newPass) {
		this.newPass = newPass;
	}
	
	public String change(){
		Map sessionMap= FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		try {
			if(!user.getPassword().equals(MD5.encrypt(oldPass))){
				sessionMap.put("err", "Haslo nie poprawne");
				return "failure";
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(oldPass.equals(newPass)){
			sessionMap.put("error", "Haslo nie moze byc takie samo");
			return "failure";
		}
		boolean result= UserLogic.getInstance().changePassword(newPass, user.getId());
		if(result)
			return "success";
		else{
			sessionMap.put("error", "Zmiana hasla nie udana");
			return "failure";
		}
			
	}

}
